package com.cglib;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotationCollectors;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * @author fanxiao  2022/3/7
 * @since 1.0.0
 */
public class AppScannerStarter {

  public static void main(String[] args) {
	  //
//	  AnnotationConfigApplicationContext context =
//			  new AnnotationConfigApplicationContext();
//
//	  context.register(AppConfig.class);
//	  context.registerBeanDefinition("eoo",
//			  BeanDefinitionBuilder.genericBeanDefinition(Eoo.class).
//					  addPropertyValue("key", "${key}").
//					  getBeanDefinition());
//	  context.refresh();
//
//
//	  AppConfig bean = context.getBean(AppConfig.class);
//	  System.out.println(bean);
//
//	  Eoo eoo = context.getBean(Eoo.class);
//	  System.out.println(eoo.getKey());

	  DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

	  beanFactory.registerBeanDefinition("eoo",
			  BeanDefinitionBuilder.genericBeanDefinition(Eoo.class).
					  addPropertyValue("key", "${key}").
					  getBeanDefinition());
	  PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
	  configurer.setProperties(new Properties()
	  {
		  {
		  	put("key", "qwe");
		  }
	  });
//	  configurer.setLocation(new ClassPathResource("data.properties"));
	  configurer.postProcessBeanFactory(beanFactory);

	  Eoo eoo1 = (Eoo) beanFactory.getBean("eoo");

	  System.out.println(eoo1.getKey());

//	  MergedAnnotation<Configuration> annotation = MergedAnnotation.of(Configuration.class);
//
//	  MergedAnnotations of = MergedAnnotations.of(Arrays.asList(annotation));
//	  System.out.println(of.isPresent(Component.class));
//	  System.out.println(of.get(Configuration.class).getBoolean("proxyBeanMethods"));
//	  List<MergedAnnotation<Annotation>> collect = of.stream().filter(MergedAnnotation::isMetaPresent).collect(Collectors.toList());
//
//	  of.stream().map(t -> t.getString("value")).forEach(System.out::println);

  }
}